package xin.codedream.codelog.web.controller.admin;

import cn.hutool.extra.servlet.ServletUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xin.codedream.codelog.model.entity.Comment;
import xin.codedream.codelog.model.entity.Logs;
import xin.codedream.codelog.model.enums.LogsEnum;
import xin.codedream.codelog.model.enums.PageInfoEnum;
import xin.codedream.codelog.model.enums.ResponseEnum;
import xin.codedream.codelog.model.global.ResponseData;
import xin.codedream.codelog.service.CommentService;
import xin.codedream.codelog.service.LogsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 文章评论控制器
 *
 * @author NGLSL
 * @date 2018/9/19
 */
@Slf4j
@RestController
@RequestMapping("/admin/comment")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private LogsService logsService;
    @Resource
    private HttpServletRequest request;

    /**
     * 显示所有文章评论
     *
     * @param commContent 评论内容
     * @param pageNum     当前页数
     * @param pageSize    页数大小
     * @return ModelAndView
     */
    @GetMapping("/showComments")
    public ModelAndView showComments(String commContent, Integer pageNum, Integer pageSize) {
        ModelAndView mv = new ModelAndView("admin/admin_comment");
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            pageSize = PageInfoEnum.PAGE_DEFAULT_NUM.getValue();
            pageNum = PageInfoEnum.PAGE_DEFAULT_SIZE.getValue();
        }
        List<Comment> allComment = commentService.findAllComment(commContent, pageNum, pageSize);
        PageInfo<Comment> pageInfo = new PageInfo<>(allComment);
        mv.addObject("allComment", allComment);
        mv.addObject("commContent", commContent);
        mv.addObject("pageNum", pageNum);
        mv.addObject("pageSize", pageSize);
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }

    /**
     * 删除评论
     *
     * @param commId 评论ID
     * @return ResponseData
     */
    @PostMapping("/deleteComment")
    public ResponseData deleteCommentData(@RequestParam Long commId) {
        ResponseData responseData = new ResponseData();
        try {
            Comment comment = commentService.findCommentById(commId);
            if (comment == null) {
                responseData.setCodeMessage(ResponseEnum.DELETE_COMMENT_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_COMMENT.getMessage(),
                        "评论ID：[" + commId + "]" + ResponseEnum.DELETE_COMMENT_ERROR.getMessage(),
                        ServletUtil.getClientIP(request)));
                return responseData;
            }
            boolean result = commentService.removeCommentById(commId);
            if (result) {
                responseData.setCodeMessage(ResponseEnum.DELETE_COMMENT_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_COMMENT.getMessage(),
                        "评论内容：[" + comment.getCommContent() + "]" + ResponseEnum.DELETE_COMMENT_SUCCESS.getMessage(),
                        ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.DELETE_COMMENT_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_COMMENT.getMessage(),
                        "评论内容：[" + comment.getCommContent() + "]" + ResponseEnum.DELETE_COMMENT_ERROR.getMessage(),
                        ServletUtil.getClientIP(request)));
            }
        } catch (Exception e) {
            log.error("删除评论失败：", e);
            responseData.setCodeMessage(ResponseEnum.DELETE_COMMENT_ERROR);
            logsService.saveLogInfo(new Logs(LogsEnum.DELETE_COMMENT.getMessage(),
                    "评论ID：[" + commId + "]" + ResponseEnum.DELETE_COMMENT_ERROR.getMessage(),
                    ServletUtil.getClientIP(request)));
        }
        return responseData;
    }

    /**
     * 更新评论状态
     *
     * @param comment 评论数据
     * @return ResponseData
     */
    @PostMapping("/updateCommStatus")
    public ResponseData updateCommentStatus(@ModelAttribute Comment comment) {
        ResponseData responseData = new ResponseData();
        try {
            Comment comm = commentService.findCommentById(comment.getCommId());
            if (comm == null) {
                responseData.setCodeMessage(ResponseEnum.UPDATE_COMMENT_STATUS_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_COMMENT_STATUS.getMessage(),
                        "评论ID：[" + comment.getCommId() + "]" + ResponseEnum.UPDATE_COMMENT_STATUS_ERROR.getMessage(),
                        ServletUtil.getClientIP(request)));
                return responseData;
            }
            boolean result = commentService.updateCommentStatusById(comment);
            if (result) {
                responseData.setCodeMessage(ResponseEnum.UPDATE_COMMENT_STATUS_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_COMMENT_STATUS.getMessage(),
                        "评论内容：[" + comm.getCommContent() + "]" + ResponseEnum.UPDATE_COMMENT_STATUS_SUCCESS.getMessage(),
                        ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.UPDATE_COMMENT_STATUS_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_COMMENT_STATUS.getMessage(),
                        "评论内容：[" + comm.getCommContent() + "]" + ResponseEnum.UPDATE_COMMENT_STATUS_ERROR.getMessage(),
                        ServletUtil.getClientIP(request)));
            }
        } catch (Exception e) {
            log.error("更新评论状态失败：", e);
            responseData.setCodeMessage(ResponseEnum.UPDATE_COMMENT_STATUS_ERROR);
            logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_COMMENT_STATUS.getMessage(),
                    "评论ID：[" + comment.getCommId() + "]" + ResponseEnum.UPDATE_COMMENT_STATUS_ERROR.getMessage(),
                    ServletUtil.getClientIP(request)));
        }
        return responseData;
    }
}
